package com.oot2d.entity;

import org.newdawn.slick.geom.Vector2f;

//A floating arbitrary point that can move to another arbitrary point
//Can "attach" another sprite, image, etc. to the point to make it move
public class Movable {
    public float x, y;

    protected Vector2f vel;
    protected boolean moving;
    protected long steps;
    protected Vector2f dest;

    public Movable()
    {
        vel = new Vector2f();
        x = 0.0f;
        y = 0.0f;
        moving = false;
    }

    public void moveTo(float px, float py, float v)
    {
        dest = new Vector2f(px, py);
        steps = (long)(Math.sqrt((dest.x - x) * (dest.x - x) + (dest.y - y) * (dest.y - y)) / v);

        float dx = px - x;
        float dy = py - y;
        float a = (float)Math.atan2(dy, dx);
        vel.x = (float)(v * Math.cos(a));
        vel.y = (float)(v * Math.sin(a));

        moving = true;
    }

    public void setPos(float px, float py)
    {
        x = px;
        y = py;
    }

    public boolean isMoving()
    {
        return moving;
    }

    public void update(int d)
    {
        if (moving) {
            steps -= d;
            if (steps <= 0) {
                steps = 0;
                moving = false;
            }

            x = (dest.x - (steps * vel.x));
            y = (dest.y - (steps * vel.y));
            return;
        }
    }
}
